package com.example.weather.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.weather.Adapter.GridViewAdapter;
import com.example.weather.R;
import com.example.weather.interfaceimpl.PhoneOperator;
import com.example.weather.utils.AppUtils;

import java.util.ArrayList;
import java.util.Map;

public class ShareActivity extends AppCompatActivity {

    private ListView mListView;
    private ArrayList<Map<String,Object>> arrayList;
    private GridViewAdapter mGridViewAdapter;
    private String content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);

        Intent intent = getIntent();
        content = intent.getStringExtra("content");

        arrayList = AppUtils.getPhoneMessage(this);
        mListView = (ListView) findViewById(R.id.lv_listView);
        mGridViewAdapter = new GridViewAdapter(this,arrayList,new PhoneOperator(),new Handler());
        mListView.setAdapter(mGridViewAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            //gridView中的每一个item的点击事件
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Map<String,Object> map = arrayList.get(position);
                sendSMS((String)map.get("phoneNumber"),content);
                Toast.makeText(ShareActivity.this,"发送成功",Toast.LENGTH_LONG).show();
                finish();
            }
        });
    }

    private void sendSMS(String phone , String smsBody)
    {
        Uri smsToUri = Uri.parse("smsto:"+phone);
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", smsBody);
        startActivity(intent);
    }
}
